using System;
using System.Windows;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
//using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;

namespace prjQlyKinhDoanh
{
    public class DB_Utility
    {
        public static void List_LoadData(ListControl lstName, string sStoreName, string sValueMember, string sDisplayMember, string sStringAdd, int iSelectedId)
        {
            DBConnect objExc = new DBConnect();
            SqlDataReader dr = objExc.returDataReader(sStoreName, null);


            if (lstName is ComboBox)
            {
                ((ComboBox)lstName).Items.Clear();
                if (sStringAdd != "")
                {
                    ((ComboBox)lstName).Items.Add(new ListItem(0, sStringAdd));
                }
                while (dr.Read())
                {
                    ((ComboBox)lstName).Items.Add(new ListItem((int)dr[sValueMember], dr[sDisplayMember].ToString()));
                }

                if (iSelectedId > 0)
                    ((ComboBox)lstName).SelectedIndex = iSelectedId;
                else
                    ((ComboBox)lstName).SelectedIndex = 0;

            }
            else
            {
                ((ListBox)lstName).Items.Clear();
                if (sStringAdd != "")
                {
                    ((ListBox)lstName).Items.Add(new ListItem(0, sStringAdd));
                }
                while (dr.Read())
                {
                    ((ComboBox)lstName).Items.Add(new ListItem((int)dr[sValueMember], dr[sDisplayMember].ToString()));
                }

                if (iSelectedId > 0)
                    ((ComboBox)lstName).SelectedIndex = iSelectedId;
                else
                    ((ListBox)lstName).SelectedIndex = 0;
            }
            dr.Close();

        }

        public static void List_LoadData(ListControl lstName, string sStoreName, IDataParameter[] parameters, string sValueMember, string sDisplayMember, string sStringAdd, int iSelectedId)
        {
            DBConnect objExc = new DBConnect();
            SqlDataReader dr = objExc.returDataReader(sStoreName, parameters);

            if (lstName is ComboBox)
            {
                ((ComboBox)lstName).Items.Clear();
                if (sStringAdd != "")
                {
                    ((ComboBox)lstName).Items.Add(new ListItem(0, sStringAdd));
                }
                while (dr.Read())
                {
                    ((ComboBox)lstName).Items.Add(new ListItem((int)dr[sValueMember], dr[sDisplayMember].ToString()));
                }

                if (iSelectedId > 0)
                    ((ComboBox)lstName).SelectedIndex = iSelectedId;
                else
                    ((ComboBox)lstName).SelectedIndex = 0;

            }
            else
            {
                ((ListBox)lstName).Items.Clear();
                if (sStringAdd != "")
                {
                    ((ListBox)lstName).Items.Add(new ListItem(0, sStringAdd));
                }
                while (dr.Read())
                {
                    ((ComboBox)lstName).Items.Add(new ListItem((int)dr[sValueMember], dr[sDisplayMember].ToString()));
                }

                if (iSelectedId > 0)
                    ((ComboBox)lstName).SelectedIndex = iSelectedId;
                else
                    ((ListBox)lstName).SelectedIndex = 0;
            }
            dr.Close();
        }

    }
}
